package org.findbugs.report.view;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.findbugs.report.entity.Bug;
import org.findbugs.report.entity.BugCollection;
import org.findbugs.report.entity.PackageStats;
import org.findbugs.report.entity.exception.ReportException;
import org.findbugs.report.messages.Messages;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

public class PDFReporter {

    private BugCollection bugCollection;
    
    public PDFReporter(BugCollection bugCollection) {
        this.bugCollection = bugCollection;
    }

    public ByteArrayOutputStream getReport() throws ReportException, DocumentException, IOException {
        // Creation of documents
        Document mainDocument = new Document(PageSize.A4, 50, 50, 140, 50);
        Document frontPageDocument = new Document(PageSize.A4, 50, 50, 140, 50);
        
        ByteArrayOutputStream mainDocumentBaos = new ByteArrayOutputStream();
        ByteArrayOutputStream frontPageDocumentBaos = new ByteArrayOutputStream();
        
        PdfWriter mainDocumentWriter = PdfWriter.getInstance(mainDocument, mainDocumentBaos);
        PdfWriter frontPageDocumentWriter = PdfWriter.getInstance(frontPageDocument, frontPageDocumentBaos);
        
        Events events = new Events(new Header(this.getClass().getResource("/logo.jpg"), bugCollection.getProject()));
        mainDocumentWriter.setPageEvent(events);
        
        mainDocument.open();
        frontPageDocument.open();
        
        printFrontPage(frontPageDocument, frontPageDocumentWriter);
        printPdfBody(mainDocument);
        
        mainDocument.close();
        frontPageDocument.close();
        
        //PdfReader mainDocumentReader = new PdfReader(mainDocumentBaos.toByteArray());
        //ByteArrayOutputStream finalBaos = new ByteArrayOutputStream();
        //PdfCopy copy = new PdfCopy(frontPageDocument, finalBaos);

        /*
        for (int i = 1; i <= mainDocumentReader.getNumberOfPages(); i++) {
            copy.addPage(copy.getImportedPage(mainDocumentReader, i));
        }
        */
        
        return mainDocumentBaos;
    }

    private void printPdfBody(Document mainDocument) throws ReportException {
        try {
        mainDocument.add(new Paragraph("Project:"));
        mainDocument.add(new Paragraph(bugCollection.getProject().getProjectName()));
        Iterator<PackageStats> it = bugCollection.getSummary().getPackageStats().iterator();
        mainDocument.add(new Paragraph("Summary:"));
        while (it.hasNext()) {
            PackageStats packageStats = it.next();
            mainDocument.add(new Paragraph("Package: " + packageStats.getPack()));
            mainDocument.add(new Paragraph("Size: " + packageStats.getSize()));
            mainDocument.add(new Paragraph("Bugs: " + packageStats.getBugs()));
            mainDocument.add(new Paragraph("------------"));
        }
        mainDocument.add(new Paragraph("\nBugs details:\n"));
        Iterator<Bug> bugsIterator = bugCollection.getBugs().iterator();
        Messages messages = new Messages();
        while (bugsIterator.hasNext()) {
            Bug bug = bugsIterator.next();
            mainDocument.add(new Paragraph("BUG"));
            mainDocument.add(new Paragraph("Category: " + messages.getCategory(bug.getCategory()).getDescription()));
            mainDocument.add(new Paragraph("Bug Pattern: " + messages.getBug(bug.getType()).getDescription()));
            mainDocument.add(new Paragraph("Class: " + bug.getClazz()));
            if (!bug.isInField()) {
                mainDocument.add(new Paragraph("Method: " + bug.getMethod()));
            } else {
                mainDocument.add(new Paragraph("Field: " + bug.getField()));
            }
            Iterator<Integer> sourceLinesIt = bug.getSourceLines().iterator();
            while (sourceLinesIt.hasNext()) {
                mainDocument.add(new Paragraph("--+ Line " + sourceLinesIt.next().toString()));
            }
            mainDocument.add(new Paragraph("------------"));

        }
        } catch (DocumentException e) {
            Logger log = Logger.getLogger(this.getClass());
            log.error(e.getMessage(), e);
            throw new ReportException(e.getMessage());
        }
    }

    private void printFrontPage(Document frontPageDocument, PdfWriter frontPageDocumentWriter) throws DocumentException {
        frontPageDocument.add(new Paragraph("Font page"));
    }
}